package view.webcam;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Image;

import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.Box;

import controler.CtrPartie;

/** Panel de la webcam
 * @author C-Net
 *
 */
public class JPWebcam extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8695102985857372638L;
	private JLabel lblWebcam;
	private JComboBox<String> comboBox;
	private JButton btnChoisir;
	private CtrPartie ctrPartie;

	/**
	 * Create the panel.
	 */
	public JPWebcam(CtrPartie p_ctrPartie) {
		this.ctrPartie = p_ctrPartie;
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{148, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 65, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		Component verticalStrut_1 = Box.createVerticalStrut(20);
		GridBagConstraints gbc_verticalStrut_1 = new GridBagConstraints();
		gbc_verticalStrut_1.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut_1.gridx = 3;
		gbc_verticalStrut_1.gridy = 0;
		add(verticalStrut_1, gbc_verticalStrut_1);
		
		JLabel lblChoisirLaWebcam = new JLabel("Choisir la webcam");
		lblChoisirLaWebcam.setFont(new Font("Tahoma", Font.PLAIN, 16));
		GridBagConstraints gbc_lblChoisirLaWebcam = new GridBagConstraints();
		gbc_lblChoisirLaWebcam.anchor = GridBagConstraints.EAST;
		gbc_lblChoisirLaWebcam.insets = new Insets(0, 0, 5, 5);
		gbc_lblChoisirLaWebcam.gridx = 0;
		gbc_lblChoisirLaWebcam.gridy = 1;
		add(lblChoisirLaWebcam, gbc_lblChoisirLaWebcam);
		
		comboBox = new JComboBox<String>();
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 1;
		gbc_comboBox.gridy = 1;
		add(comboBox, gbc_comboBox);
		
		btnChoisir = new JButton("Choisir");
		GridBagConstraints gbc_btnChoisir = new GridBagConstraints();
		gbc_btnChoisir.insets = new Insets(0, 0, 5, 5);
		gbc_btnChoisir.gridx = 2;
		gbc_btnChoisir.gridy = 1;
		add(btnChoisir, gbc_btnChoisir);
		btnChoisir.addActionListener(this);
		
		Component verticalStrut = Box.createVerticalStrut(20);
		GridBagConstraints gbc_verticalStrut = new GridBagConstraints();
		gbc_verticalStrut.insets = new Insets(0, 0, 5, 0);
		gbc_verticalStrut.gridx = 3;
		gbc_verticalStrut.gridy = 2;
		add(verticalStrut, gbc_verticalStrut);
		
		lblWebcam = new JLabel("");
		GridBagConstraints gbc_lblWebcam = new GridBagConstraints();
		gbc_lblWebcam.insets = new Insets(0, 0, 0, 5);
		gbc_lblWebcam.gridwidth = 3;
		gbc_lblWebcam.fill = GridBagConstraints.BOTH;
		gbc_lblWebcam.gridx = 0;
		gbc_lblWebcam.gridy = 3;
		add(lblWebcam, gbc_lblWebcam);
	}
	
	public void setWebcams(ArrayList<String> webcams){
		for(int i=0 ; i<webcams.size() ; ++i){
			comboBox.addItem(webcams.get(i));
		}
	}

	public void setImage(Image i){
		this.lblWebcam.setIcon(new ImageIcon(i));
	}

	@Override
	public void actionPerformed(ActionEvent btn) {
		if(btn.getSource().equals(this.btnChoisir)){
			int i = this.comboBox.getSelectedIndex();
			if(i != -1){
				this.ctrPartie.getSender().sendOrder("webcam,"+i);
			}
		}
	}
}
